﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using ProgNetDxComponents.ExtenderProviders.Settings;

namespace ProgNetDxComponents.ExtenderProviders
{
    public class NullInputProvider : ProgNetComponents.ExtenderProviders.BaseExtenderProvider<Settings.NullInputSettings>
    {
        public NullInputProvider() { }
        public NullInputProvider(System.ComponentModel.IContainer container)
            : base(container)
        {

        }

        protected override void ProcessObject(object sender, Settings.NullInputSettings settings)
        {
            if (sender is BaseEdit)
            {
                (sender as BaseEdit).Properties.KeyDown += new System.Windows.Forms.KeyEventHandler(Properties_KeyDown);
            }
            else if (sender is RepositoryItem)
            {
                (sender as RepositoryItem).KeyDown += new System.Windows.Forms.KeyEventHandler(Properties_KeyDown);
                (sender as RepositoryItem).Tag = sender;
            }
        }
        protected override object GetRealKey(object key)
        {
            object res = key;
            if (key is RepositoryItem && (key as RepositoryItem).Tag is RepositoryItem)
                res = (key as RepositoryItem).Tag;
            return res;
        }

        void Properties_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Delete)
            {
                NullInputSettings s = null;
                BaseEdit ed = sender as BaseEdit;
                if (_AllSettings.ContainsKeySafe(ed))
                    s = _AllSettings[ed];
                else
                    s = GetSettings(ed.Properties);
                if (s != null && s.ClearOnDelete && !ed.Properties.ReadOnly)
                {
                    if (s.ClearType == ClearType.SetEmptyString)
                        ed.EditValue = string.Empty;
                    if (s.ClearType == ClearType.SetNull)
                        ed.EditValue = null;
                }
            }
        }

        public override bool CanExtend(object extendee)
        {
            if ((extendee is ButtonEdit ||
                extendee is RepositoryItemButtonEdit )&&
                !(extendee is MemoExEdit ||
                extendee is RepositoryItemMemoExEdit))
                return true;
            return false;
        }
    }
}
